{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1094c37a-4d4e-4d68-b8e3-c2f5d0378e7f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b38cf69d-9ecd-4c90-a4ca-0b000fd50b2e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python python\n",
      "python\n",
      "java\n",
      "JLNU\n",
      "SIPING\n",
      "JILIN\n",
      "China\n"
     ]
    }
   ],
   "source": [
    "s1 = \"python\"\n",
    "s2 = \"python\"\n",
    "print(s1,s2)\n",
    "\n",
    "s3 = 'python\\njava'\n",
    "print(s3)\n",
    "'''I love Python\n",
    "I love jlnu\n",
    "I love Siping'''\n",
    "print(\"\"\"JLNU\n",
    "SIPING\n",
    "JILIN\n",
    "China\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "eb9336ae-6f7e-4557-9ee5-0eee897e8f66",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefg fsdfhdcygfh\n",
      "\\ I'm a student \"\n",
      "abcdef hijklmn\n",
      "I love Python\n",
      "\tJava\n"
     ]
    }
   ],
   "source": [
    "s = 'abcdefg \\\n",
    "fsdfhdcygfh'\n",
    "print(s)\n",
    "print('\\\\',\"I\\'m a student\",'\\\"')\n",
    "import time\n",
    "print('abcdefg\\b hijklmn')\n",
    "print('I love Python\\r', end = '\\n')\n",
    "print('\\tJava')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "08bcfb07-0348-4145-a276-e203f8d3fb91",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1&2@"
     ]
    }
   ],
   "source": [
    "print(1, 2, sep='&', end='@')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "249b824f-c8aa-4e9b-a498-b8e569742a62",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "abcefg\n",
      "Hi!Hi!Hi!Hi!\n",
      "--------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "print(len('python'))\n",
    "print('abc'+'efg')\n",
    "print('Hi!'*4)\n",
    "print('-'*80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8dc2c5d6-b606-419f-8146-0987b29dd00b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6+9\n"
     ]
    }
   ],
   "source": [
    "print('6+9')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0e45b582-32a1-4b56-8a9c-981c7a99ee79",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17\n"
     ]
    }
   ],
   "source": [
    "print(8+9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a59b4b8d-e490-4258-9944-8375fae71636",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 H\n",
      "1 a\n",
      "2 c\n",
      "3 k\n",
      "4 e\n",
      "5 r\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#字符串的遍历\n",
    "myjob = \"Hacker\"\n",
    "for i, c in enumerate (myjob):#使用enumerate函数输出索引值\n",
    "    print(i ,c)\n",
    "#字符串包含测试in\n",
    "print('Hacker' in myjob)\n",
    "print('job' in myjob)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b216566e-b0a8-4202-b247-afa2cf7df474",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s p\n",
      "n\n",
      "love\n",
      "I love I love\n",
      " python\n",
      "1289213639408 1289213639408\n",
      "Ilv yhn\n",
      "nohtyp evol I\n",
      "I love python\n"
     ]
    }
   ],
   "source": [
    "#字符串索引\n",
    "s = 'spam'\n",
    "print(s[0],s[1])\n",
    "s1 = 'I love python'\n",
    "print(s1[-1])#取字符串最后的元素\n",
    "\n",
    "#字符串切片\n",
    "print(s1[2:6])#不包含最后一个数字的索引位置\n",
    "print(s1[0:6],s1[:6])#从字符串初始位置切片，首索引可以省略\n",
    "print(s1[6:]) #从某个索引位起，到最后一个字符\n",
    "print(id(s1[:]),id(s1))#s1[:]和s1是同一个字符串\n",
    "print(s1[::2])#步长\n",
    "print(s1[::-1])#字符串反转\n",
    "print(s1[::])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a48b8fe1-c4eb-48c5-bb46-4efec0e67245",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42 43\n",
      "97 a\n"
     ]
    }
   ],
   "source": [
    "#字符串转换数字\n",
    "print(int('42'),str(43))\n",
    "#ord函数：字符转数字，chr函数：数字转字符\n",
    "#python语言所有字符使用的是unicode编码，包含世界上所有语言的常用字符\n",
    "print(ord('a'),chr(97))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "554adbef-29c5-413f-bf26-8166c8508e06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We love JINU\n"
     ]
    }
   ],
   "source": [
    "#字符串是可变的\n",
    "s = 'I love JINU'\n",
    "# s[0] = 'Y' 不能用索引的方式改变\n",
    "s = 'We' + s[1:] #不是改变了字符串本身，而是变量重新赋值了\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c78b6a99-31ec-48c6-b5c2-c57dddff7996",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'I love Python'\n",
      "<class 'bytes'>\n",
      "b'\\xce\\xd2\\xb0\\xae\\xbc\\xaa\\xc1\\xd6\\xca\\xa6\\xb7\\xb6\\xb4\\xf3\\xd1\\xa7'\n",
      "我爱吉林师范大学\n"
     ]
    }
   ],
   "source": [
    "#字符串str的encode方法\n",
    "s = 'I love Python'\n",
    "print(s.encode('utf-8'))\n",
    "print(type(s.encode('utf-8')))\n",
    "s = '我爱吉林师范大学'\n",
    "\n",
    "\n",
    "s_gbk = s.encode('gbk')\n",
    "print(s_gbk)\n",
    "print(s_gbk.decode('gbk'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9fb190d2-8cd5-453e-b2ac-cbf055dbf432",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：王老五，年龄：20\n"
     ]
    }
   ],
   "source": [
    "#字符串格式化方式\n",
    "# 1 format 书100页\n",
    "\n",
    "# 2 f方法\n",
    "name = '王老五'\n",
    "age = 20\n",
    "# format写法\n",
    "s = \"姓名：{}，年龄：{}\".format(name,age)\n",
    "# 流行的f写法\n",
    "print(f\"姓名：{name}，年龄：{age}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cf4b3035-659f-4bfd-89d3-5fe610f73610",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 2\n",
      "find -1\n",
      "11\n",
      "2 0\n"
     ]
    }
   ],
   "source": [
    "#字符串查找的相关方法\n",
    "#find rfind index rindex count\n",
    "s = 'I love Python'\n",
    "#查找成功的情况下，find和index是一样的\n",
    "print(s.find('love'),s.index('love'))\n",
    "#查找不成功的情况下，\n",
    "print('find',s.find('lave')) #find返回-1\n",
    "#print('index',s.index('lave')) #index抛出ValueError错误\n",
    "print(s.find('o',4)) #找到第一个子串的位置，可以指定起始和结束位置\n",
    "print(s.count('o'),s.count('a')) #返回字串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "08039a6a-23af-4cbb-ba97-97beaa79b7ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'C', 'C++', 'Java']\n",
      "['China', 'Russia', 'USA', 'Canda']\n",
      "('China', ',', 'Russia,USA,Canda')\n",
      "('I love Python\\nI love Java\\nIl love CLang', '', '')\n"
     ]
    }
   ],
   "source": [
    "# 字符串分隔函数\n",
    "# split, rsplit, partition, rpartition\n",
    "\n",
    "s = 'python \\n C      C++\\t  Java'\n",
    "print(s.split())  # 默认用空白进行分隔，空白包括空格，换行，制表符\n",
    "s1 = 'China,Russia,USA,Canda'\n",
    "print(s1.split(','))\n",
    "print(s1.partition(','))\n",
    "\n",
    "s2 = 'I love Python\\nI love Java\\nIl love CLang'\n",
    "print(s2.partition(','))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d1de578c-b8bc-4ce2-9da4-67c1e8976c92",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "China USA Janpan\n",
      "apple,prach,banana\n"
     ]
    }
   ],
   "source": [
    "# 字符串连接方法\n",
    "print(\" \".join(['China','USA','Janpan']))\n",
    "print(','.join(('apple','prach','banana')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9c1e1ef5-22ac-4bfe-9835-62a92b987eb9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love python I LOVE PYTHON\n",
      "I love python\n",
      "I Love Python\n",
      "I Love PythON\n",
      "i love python\n"
     ]
    }
   ],
   "source": [
    "#字符串大小写的一些方法\n",
    "s = 'I Love Python'\n",
    "print(s.lower(),s.upper())\n",
    "print(s.capitalize())\n",
    "print(s.title())\n",
    "s1 = 'i lOVE pYTHon'\n",
    "print(s1.swapcase())#小写变大写\n",
    "print(s1.casefold())#大写变小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "599a30bf-e1d3-4f19-92c2-0058e27847a1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello World\n",
      "hello World \n",
      " \n",
      " \t hello World\n"
     ]
    }
   ],
   "source": [
    "#字符串去掉两端的空白或指定字符\n",
    "s = '\\n \\t hello World \\n'\n",
    "print(s.strip()) #去掉两端空白\n",
    "print(s.lstrip(),s.rstrip())  #去掉左端空白，去掉右端空白"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e4e9703f-2803-43b0-b9f5-e4268df839ff",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "abc_python_bcd\n",
      "abc_python_\n"
     ]
    }
   ],
   "source": [
    "#字符串判断是否以字符串开头或结束\n",
    "s = 'China USA'\n",
    "print(s.startswith('China'))\n",
    "print(s.endswith('SA'))\n",
    "\n",
    "#去掉点后缀\n",
    "s = \"abc_python_bcd\"\n",
    "print(s.removeprefix('abcd')) #去掉前缀,如果找到\n",
    "print(s.removesuffix('bcd'))  #去掉后缀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c7925cb-2ad8-4616-b4a1-64ba52020121",
   "metadata": {},
   "outputs": [],
   "source": [
    "#isupper isspace空白 istitle首字母大写  isprintable看不到  isascir是否是as码  isalpha是否都是字母\n",
    "#isalnum是否是字母表里的有数字也是true  isnumeric表数字的都行什么语言都行 isdecimal，isdigit阿拉伯数字\n",
    "#"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
